import {
  PutRes,
  Access,
  AccessAll,
  DisableRes,
  putAccessDto,
  AccessGateway,
  searchAccessDto,
  deleteAccessDto,
  getAccessDroitContextDto,
  AccessInterfaceWithDerive,
  AccessInterfaceWithPrimary,
} from "qqlx-core";

import { AccessSchema } from "@/stream/schema-access";
import { ViewBase, ViewBaseAction } from "@view/view-base";

export class ViewAccess extends ViewBase<Access> implements ViewBaseAction<Access>, AccessInterfaceWithPrimary<{}>, AccessInterfaceWithDerive<{}> {
  private static instance: ViewAccess;
  static getInstance(options?: { isSingleton?: boolean }) {
    if (options?.isSingleton === false) {
      return new ViewAccess();
    }

    if (!this.instance) this.instance = new ViewAccess();
    return this.instance;
  }

  private constructor() {
    super();
    this.initial();
    this.insertDefaultCondition();
  }

  getSchema(): Access {
    return new AccessSchema();
  }

  async putAccess(dto: putAccessDto) {
    this.pending();
    return this.$request.put<putAccessDto, PutRes<Access>>(AccessGateway.path, dto);
  }

  async deleteAccess(dto: deleteAccessDto) {
    this.pending();
    return this.$request.delete<deleteAccessDto, DisableRes>(AccessGateway.path, dto);
  }

  async search(dto: searchAccessDto) {
    this.pending();
    const path = `${AccessGateway.path}/search`;
    return this.$request.put<searchAccessDto, AccessAll>(path, dto);
  }

  async getAccessDroitContext(dto: getAccessDroitContextDto) {
    this.pending();
    const path = `${AccessGateway.path}/droit/context`;
    return this.$request.getByPut<getAccessDroitContextDto, { entity?: Access }>(path, dto);
  }
}
